package zcw.com.lib_leet_code;

import java.util.Arrays;
import java.util.List;

import zcw.com.lib_leet_code.bean.TreeNode;
import zcw.com.lib_leet_code.util.Util;

/**
 * Created by 朱城委 on 2021/7/7.<br><br>
 */
public class Topic101 {
    public static void main(String[] args) {
        Topic101 instance = new Topic101();

        List<Integer> list = Arrays.asList(1, 2, 2, null, 3, null, 3);
        TreeNode root = Util.createTree(list);

        Util.preTravelTree(root);
        System.out.println();

        Util.inTravelTree(root);
        System.out.println();

        Util.postTravelTree(root);
        System.out.println();

        Util.levelTravelTree(root);
        System.out.println();

        System.out.println(instance.isSymmetric(root));

        list = Arrays.asList(1,2,2,3,4,4,3);
        root = Util.createTree(list);
        System.out.println(instance.isSymmetric(root));


    }

    public boolean isSymmetric(TreeNode root) {
        return compareLeftRightTree(root.left, root.right);
    }

    private boolean compareLeftRightTree(TreeNode leftTree, TreeNode rightTree) {
        if(leftTree == null && rightTree == null) {
            return true;
        }

        if(leftTree == null) {
            return false;
        }

        if(rightTree == null) {
            return false;
        }

        return leftTree.val == rightTree.val && compareLeftRightTree(leftTree.left, rightTree.right) &&
                compareLeftRightTree(leftTree.right, rightTree.left);
    }
}
